package edu.whu.demo.service;

import edu.whu.demo.entity.Commodity;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CommodityService {
    private Map<String, Commodity> commodities =
            Collections.synchronizedMap(new HashMap<String, Commodity>());

    public Commodity addCommodity(Commodity commodity) {
        commodities.put(commodity.getId(), commodity);
        return commodity;
    }

    public Commodity getCommodity(String id) {
        return commodities.get(id);
    }

    public void updateCommodity(String id, Commodity commodity) {
        Commodity c1  = commodities.get(id);
        c1.setName(commodity.getName());
        commodities.put(id, commodity);
    }

    public void deleteCommodity(String id) {
        commodities.remove(id);
    }
    public void reduceCommodity(String  id){
        commodities.get(id).setQuantity(commodities.get(id).getQuantity()-1);
    }
    public void addCommodity(String  id){
        commodities.get(id).setQuantity(commodities.get(id).getQuantity()+1);
    }


}
